package com.doge.service.admin.address;

import com.alibaba.druid.util.StringUtils;
import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.pojo.address.Address;
import com.doge.bean.pojo.address.AddressExample;
import com.doge.mapper.AddressMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project2-wechat-mall
 * @description: 会员地址
 * @author: zhu lei
 * @create: 2021-11-30 15:11
 **/
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;

    @Override
    public BaseData addressList(Integer userId, String name, BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        addressExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        PageInfo<Address> addressPageInfo = new PageInfo<>(addresses);
        long total = addressPageInfo.getTotal();
        return BaseData.list(addresses,total);
    }
}